/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View,
  TextInput,
  TouchableOpacity,
} from 'react-native';

import RNWavecom from 'react-native-wavecom';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

type Props = {};
export default class App extends Component<Props> {

  constructor(){
    super();
    this.state = {
      ssid: "123456",
      pass: "456123",
      sending: false,
    }
    this.times = 2;
    this.count = 0;
    RNWavecom.setBaseFrequency(4000);
    RNWavecom.addEventListener(RNWavecom.WAVECOM_ON_END,()=>{
      console.log("wavecom on end");
      if(++this.count == this.times){
        this.count = 0;
        this.setState({
          sending: false,
        });
      }
    });
  }

  render() {
    return (
      <View style={styles.container}>
      <TextInput
        style={styles.ti}
          placeholder={"ssid"}
          //underlineColorAndroid='transparent'
          clearButtonMode="while-editing"
          onChangeText={(text) => {
            this.setState({
              ssid: text
            });
          }}
          value={this.state.ssid}
        />
        <TextInput
        style={styles.ti}
          placeholder={"pass"}
          //underlineColorAndroid='transparent'
          clearButtonMode="while-editing"
          onChangeText={(text) => {
            this.setState({
              pass: text
            });
          }}
          value={this.state.pass}
        />
        <TouchableOpacity disabled={this.state.sending} onPress={()=>{
          
          RNWavecom.sendWifi(this.state.ssid,this.state.pass,this.times,1000).then(()=>{
            this.setState({
              sending: true
            });
          }).catch((err)=>{
            console.log(err);
          });
          }}>
          <Text>发送</Text>
          </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
  ti: {
    width: 200,
  }
});
